public class GetIntersectionNode {
    // leetcode 相交链表
    // https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-liked
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int count1 = 0, count2 = 0;
        ListNode cur1 = headA, cur2 = headB;
        while (cur1 != null) {
            cur1 = cur1.next;
            count1++;
        }
        while (cur2 != null) {
            cur2 = cur2.next;
            count2++;
        }
        if (count1 > count2) {
            for (int i = 0; i < count1 - count2; i++) {
                headA = headA.next;
            }
        }else {
            for (int i = 0; i < count2 - count1; i++) {
                headB = headB.next;
            }
        }
        while (headA != null && headB != null) {
            if (headA == headB) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
}
